package net.xdclass.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.xdclass.enums.ShortLinkStateEnum;
import net.xdclass.vo.GroupCodeMappingVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.xdclass.model.GroupCodeMappingDO;
import net.xdclass.mapper.GroupCodeMappingMapper;
import net.xdclass.service.GroupCodeMappingService;

/**
 * @author Allen
 * @version 1.0
 * @date 2022/11/11 21:41
 */
@Service
public class GroupCodeMappingServiceImpl extends ServiceImpl<GroupCodeMappingMapper, GroupCodeMappingDO> implements GroupCodeMappingService {

    /**
     * 查找详情
     *
     * @param mappingId
     * @param accountNo
     * @param groupId
     * @return
     */
    @Override
    public GroupCodeMappingDO findByGroupIdAndMappingId(Long mappingId, Long accountNo, Long groupId) {

        return this.lambdaQuery()
                .eq(GroupCodeMappingDO::getId, mappingId)
                .eq(GroupCodeMappingDO::getAccountNo, accountNo)
                .eq(GroupCodeMappingDO::getGroupId, groupId)
                .one();

    }

    /**
     * 新增
     *
     * @param groupCodeMappingDO
     * @return
     */
    @Override
    public int add(GroupCodeMappingDO groupCodeMappingDO) {
        return this.baseMapper.insert(groupCodeMappingDO);
    }

    /**
     * 根据短链码删除
     *
     * @param shortLinkCode
     * @param accountNo
     * @param groupId
     * @return
     */
    @Override
    public int del(String shortLinkCode, Long accountNo, Long groupId) {
        return this.baseMapper.delete(
                this.lambdaQuery()
                        .eq(GroupCodeMappingDO::getCode, shortLinkCode)
                        .eq(GroupCodeMappingDO::getAccountNo, accountNo)
                        .eq(GroupCodeMappingDO::getGroupId, groupId)
        );
    }

    /**
     * 分页查找
     *
     * @param page
     * @param size
     * @param accountNo
     * @param groupId
     * @return
     */
    @Override
    public Map<String, Object> pageShortLinkByGroupId(Integer page, Integer size, Long accountNo, Long groupId) {

        Page<GroupCodeMappingDO> pageInfo = new Page<>(page, size);

        Page<GroupCodeMappingDO> groupCodeMappingDOPage = this.baseMapper.selectPage(pageInfo, new QueryWrapper<GroupCodeMappingDO>().eq("account_no", accountNo)
                .eq("group_id", groupId));

        Map<String, Object> pageMap = new HashMap<>(3);

        pageMap.put("total_record", groupCodeMappingDOPage.getTotal());
        pageMap.put("total_page", groupCodeMappingDOPage.getPages());
        pageMap.put("current_data", groupCodeMappingDOPage.getRecords()
                .stream().map(obj -> beanProcess(obj)).collect(Collectors.toList()));


        return pageMap;
    }

    /**
     * 更新短链码状态
     *
     * @param accountNo
     * @param groupId
     * @param shortLinkCode
     * @param shortLinkStateEnum
     * @return
     */
    @Override
    public int updateGroupCodeMappingState(Long accountNo, Long groupId, String shortLinkCode, ShortLinkStateEnum shortLinkStateEnum) {
        int rows = this.baseMapper.update(null, new UpdateWrapper<GroupCodeMappingDO>()
                .eq("code", shortLinkCode).eq("account_no", accountNo)
                .eq("group_id", groupId).set("state", shortLinkStateEnum.name()));

        return rows;
    }



    private GroupCodeMappingVO beanProcess(GroupCodeMappingDO groupCodeMappingDO) {
        GroupCodeMappingVO groupCodeMappingVO = new GroupCodeMappingVO();
        BeanUtils.copyProperties(groupCodeMappingDO, groupCodeMappingVO);

        return groupCodeMappingVO;
    }

}
